package com.ferryc;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Random;

/**
 * Created by Ferry Chen on 2019/11/6.
 */
public class Account {

    private RestTemplate restTemplate = new RestTemplate();
    private String url = "http://127.0.0.1:8087/account";

    private String user_url = "http://127.0.0.1:8087/user";

    @Test
    public void add() throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        ResponseEntity<String> resp = restTemplate.getForEntity(user_url + "/list", String.class);
        JSONObject res = JSONObject.parseObject(resp.getBody());
        JSONArray jarr = (JSONArray) res.get("data");
        MultiValueMap<String, Object> map = null;
        for (int i = 0; i < jarr.size(); i++) {
            JSONObject j = (JSONObject) jarr.get(i);
            map = new LinkedMultiValueMap<>();
            map.add("userId", j.get("id"));
            map.add("money", getRandom() + 1000.0);
            HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
            ResponseEntity<String> result = restTemplate.postForEntity(url + "/add", request, String.class);
            System.out.println(result.getBody());
            map = null;
        }
    }

    @Test
    public void consume() throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        ResponseEntity<String> resp = restTemplate.getForEntity(user_url + "/list", String.class);
        JSONObject res = JSONObject.parseObject(resp.getBody());
        JSONArray jarr = (JSONArray) res.get("data");
        MultiValueMap<String, Object> map = null;
        for (int i = 0; i < jarr.size(); i++) {
            JSONObject j = (JSONObject) jarr.get(i);
            map = new LinkedMultiValueMap<>();
            map.add("userId", j.get("id"));
            map.add("money", getRandom() + 200.5);
            HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
            ResponseEntity<String> result = restTemplate.postForEntity(url + "/consume", request, String.class);
            System.out.println(result.getBody());
            map = null;
        }
    }
    @Test
    public void update() throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        ResponseEntity<String> resp = restTemplate.getForEntity(user_url + "/list", String.class);
        JSONObject res = JSONObject.parseObject(resp.getBody());
        JSONArray jarr = (JSONArray) res.get("data");
        MultiValueMap<String, Object> map = null;
        for (int i = 0; i < jarr.size(); i++) {
            JSONObject j = (JSONObject) jarr.get(i);
            map = new LinkedMultiValueMap<>();
            map.add("userId", j.get("id"));
            map.add("money", String.valueOf(getRandom() + 9000.3));
            HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
            ResponseEntity<String> result = restTemplate.postForEntity(url + "/update", request, String.class);
            System.out.println(result.getBody());
            map = null;
        }
    }

    private int getRandom() {
        Random random = new Random();
        return random.nextInt(100);
    }
}
